//Regressions using quarterly data 2004-2018

clear all
set more off
qui use GU04_18_Q_j_all, replace
qui tsset idj yrq2, quarterly 

drop ft_*  union_* own_* inc_* profit_*
keep if j_ft==int(j_ft)
qui tab j_ft, gen(ft_)
keep if j_union==int(j_union)
qui tab j_union, gen(union_)
keep if j_ownership==int(j_ownership)
qui tab j_ownership, gen(own_)
keep if j_inc==int(j_inc)
qui tab j_inc, gen(inc_)
keep if j_profit==int(j_profit)
qui tab j_profit, gen(profit_)

drop gdpsize_* gdpfemog_* gdpn1_* gdprg_* gdpft_* gdp gdpunion_* gdpown_* gdpinc_* gdpprofit_* 
foreach a of varlist size_* femog_* n1_* rg_* ft_*  union_* own_* inc_* profit_* ivf_* {
qui gen gdp`a'=`a'*logrgdpdiff
}

drop if missing(logdiffj_ahr) | missing(logdiffj_cqtotben) | missing(logdiffj_comp)

qui tsset idj yrq2, quarterly 
sort idj cycle
qui egen wtcon=mean(j_estoccwt), by (idj)

qui gen dp7=0
qui replace dp7=1 if year>2007
foreach a of varlist logrgdpdiff {
qui gen dp7`a'=`a'*dp7
}

//============================================================================

//Table 3
foreach a of varlist j_ahr j_cqtotben j_comp { 
eststo: capture noisily areg logdiff`a' logrgdpdiff cycle dp7logrgdpdiff dp7 inc_* profit_* j_ivf_total_points l.logemplydiff size_* ft_* femog_* n1_* rg_*  union_* own_* [pw=j_estoccwt], absorb(idj) vce(cluster n2)  
eststo: capture noisily xtreg logdiff`a' logrgdpdiff cycle dp7logrgdpdiff dp7 inc_* profit_* j_ivf_total_points l.logemplydiff size_* ft_* femog_* n1_* rg_*  union_* own_* [pw=wtcon], fe vce(cluster n2)  
esttab, se ar2 stats(N r2)
estimates clear
}

//Table 7: Post-07 only: every year dum
qui gen dum8=0
qui gen dum9=0
qui gen dum10=0
qui gen dum11=0
qui gen dum12=0
qui gen dum13=0
qui gen dum14=0
qui gen dum15=0
qui gen dum16=0
qui gen dum17=0
qui gen dum18=0
qui replace dum8=1 if year==2008
qui replace dum9=1 if year==2009
qui replace dum10=1 if year==2010
qui replace dum11=1 if year==2011
qui replace dum12=1 if year==2012
qui replace dum13=1 if year==2013
qui replace dum14=1 if year==2014
qui replace dum15=1 if year==2015
qui replace dum16=1 if year==2016
qui replace dum17=1 if year==2017
qui replace dum18=1 if year==2018

foreach a of varlist dum8 dum9 dum10 dum11 dum12 dum13 dum14 dum15 dum16 dum17 dum18 {
qui gen rgdp`a'=`a'*logrgdpdiff 
}

qui egen mean_hplogrgdp=mean(hplogrgdp)
qui gen dumblw_meanhprgdp=0
qui replace dumblw_meanhprgdp=1 if hplogrgdp<mean_hplogrgdp
qui gen dumblw_logrgdpdiff=logrgdpdiff*dumblw_meanhprgdp

qui egen perct = xtile(logj_ahr), by(year) p(1(1)99)
qui gen perctsq = perct^2

sort idj yrq2

foreach a of varlist j_ahr j_cqtotben j_comp { 
eststo: capture noisily areg logdiff`a' cycle rgdpdum8 rgdpdum9 rgdpdum10 rgdpdum11 rgdpdum12 rgdpdum13 rgdpdum14 rgdpdum15 rgdpdum16 rgdpdum17 rgdpdum18 dum8 dum9 dum10 dum11 dum12 dum13 dum14 dum15 dum16 dum17 femog_* n1_* rg_*  union_* own_* if year>2007 [pw=j_estoccwt], absorb(idj) vce(cluster n2)  
eststo: capture noisily xtreg logdiff`a' cycle rgdpdum8 rgdpdum9 rgdpdum10 rgdpdum11 rgdpdum12 rgdpdum13 rgdpdum14 rgdpdum15 rgdpdum16 rgdpdum17 rgdpdum18 dum8 dum9 dum10 dum11 dum12 dum13 dum14 dum15 dum16 dum17 femog_* n1_* rg_*  union_* own_* if year>2007 [pw=wtcon], fe vce(cluster n2)  
esttab, se ar2 stats(N r2)
estimates clear
}
